import { request } from '@/service/request';

/** 数据模型 */
export interface DataModel {
  id?: number;
  tenantId?: number;
  name: string;
  tableName: string;
  tableType: string;
  partitionType?: string;
  partitionColumn?: string;
  bucketColumn?: string;
  bucketNum?: number;
  fields: ModelField[];
  description?: string;
  version?: number;
  dorisDatabase?: string;
  dorisTable?: string;
  syncStatus?: number;
  syncMessage?: string;
  status?: number;
  projectId?: number;
  createTime?: string;
  updateTime?: string;
  remark?: string;
}

/** 模型字段 */
export interface ModelField {
  name: string;
  type: string;
  length?: number;
  scale?: number;
  nullable?: boolean;
  primaryKey?: boolean;
  defaultValue?: string;
  comment?: string;
  aggregateType?: string;
}

/** 数据模型DTO */
export interface DataModelDTO {
  id?: number;
  name: string;
  tableName: string;
  tableType: string;
  partitionType?: string;
  partitionColumn?: string;
  bucketColumn?: string;
  bucketNum?: number;
  fields: ModelField[];
  description?: string;
  projectId?: number;
  changeDescription?: string;
  syncToDoris?: boolean;
}

/** 模型版本历史 */
export interface DataModelVersion {
  id: number;
  modelId: number;
  version: number;
  fields: string;
  changeDescription?: string;
  createBy?: string;
  createTime?: string;
}

/**
 * 查询数据模型列表
 */
export function getDataModelList(params?: any) {
  return request<ApiRes<PageRes<DataModel>>>({
    url: '/dataplatform/datamodel/list',
    method: 'get',
    params
  });
}

/**
 * 获取数据模型详情
 */
export function getDataModel(id: number) {
  return request<ApiRes<DataModel>>({
    url: `/dataplatform/datamodel/${id}`,
    method: 'get'
  });
}

/**
 * 新增数据模型
 */
export function addDataModel(data: DataModelDTO) {
  return request<ApiRes<number>>({
    url: '/dataplatform/datamodel',
    method: 'post',
    data
  });
}

/**
 * 修改数据模型
 */
export function updateDataModel(data: DataModelDTO) {
  return request<ApiRes<void>>({
    url: '/dataplatform/datamodel',
    method: 'put',
    data
  });
}

/**
 * 删除数据模型
 */
export function deleteDataModel(ids: number | number[]) {
  return request<ApiRes<void>>({
    url: `/dataplatform/datamodel/${ids}`,
    method: 'delete'
  });
}

/**
 * 验证数据模型
 */
export function validateDataModel(data: DataModelDTO) {
  return request<ApiRes<boolean>>({
    url: '/dataplatform/datamodel/validate',
    method: 'post',
    data
  });
}

/**
 * 同步到Doris
 */
export function syncToDoris(id: number) {
  return request<ApiRes<void>>({
    url: `/dataplatform/datamodel/${id}/sync`,
    method: 'post'
  });
}

/**
 * 查询版本历史
 */
export function getVersionHistory(id: number) {
  return request<ApiRes<DataModelVersion[]>>({
    url: `/dataplatform/datamodel/${id}/versions`,
    method: 'get'
  });
}

/**
 * 更新状态
 */
export function updateDataModelStatus(id: number, status: number) {
  return request<ApiRes<void>>({
    url: `/dataplatform/datamodel/${id}/status/${status}`,
    method: 'put'
  });
}
